<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list" :default-sort="{ prop: 'workNumber' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template slot-scope="scope">
              {{ format(scope.row.formOfEmployment) }}
              <!-- <button @click="f(scope)" /> -->
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="账户状态" prop="correctionTime" />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="doDel(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <div style="height: 60px; margin-top: 10px">
            <!-- 分页 -->
            <el-pagination
              layout="total, sizes,prev, pager, next"
              :total="total"
              :page-size="pagesize"
              :page-sizes="[1, 2, 5, 10]"
              :current-page="page"
              @current-change="hCurrentChange"
              @size-change="hSizeChange"
            />
          </div>
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getEmployeeList, delEmployess } from '@/api/employees'
const EMPE_CODE = { 1: '正式', 2: '非正式' }
export default {
  data() {
    return {
      list: [],
      // 当前第几页
      page: 1,
      // 每页多少条
      pagesize: 2,
      // 总共多少条
      total: 0
    }
  },
  created() {
    this.loadEmployeeList()
    this.format()
  },
  methods: {
    async doDel(id) {
      await delEmployess(id)
      this.$message.success('删除成功')
      this.loadEmployeeList()
    },
    // 跳转到第几页
    hCurrentChange(val) {
      this.page = val
      this.loadEmployeeList()
    },
    // 改变每一页多少条
    hSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.loadEmployeeList()
    },
    format(type) {
      return EMPE_CODE[type] || '未知'
    },
    f(scope) {
      console.log(scope)
    },
    async loadEmployeeList() {
      // const res = await getEmployeeList({ page: 1, pagesize: 10 })
      const res = await getEmployeeList({
        page: this.page,
        size: this.pagesize
      })
      this.list = res.data.rows
      this.total = res.data.total
      console.log(res)
    }
  }
}
</script>
